
import * as network_utils from '../../../../../http/NetworkUtils';
import * as constants from '../../../../../common/Constants';
import * as umfutils from '../../../../../utils/UmfUtils';
import * as RegexUtils from '../../../../../utils/RegexUtils';
import { saveUserCache } from '../../../../../model/CacheUtils';

import { $SendMailPop, $Toast } from '../../../../../components/CompRouter';

var pageNo = "1";
var pageSize = "10";
var agentId = "";
var profitDate = "";
var email = "";
const app = getApp();

Page({

    data: {
        //分润类型
        profitType: '',
        //店铺id，决定是否显示导出按钮
        storeId: '',
        //列表数据
        detailDataList: _getDetailList()
    },

    onLoad: function (options) {
        this.setData({
            profitType: constants.ProfitTypes[options.profitType],
            storeId: options.storeId
        });

        pageNo = "1";
        profitDate = options.date;
        agentId = options.agentId;

        //设置标题
        wx.setNavigationBarTitle({
            title: "分润查询(" + options.date + ")"
        });

        //查询明细数据
        this._requestDetails();
    },

    /**
     * 导出对账邮件
     */
    onExport: function () {
        let userCache = app.globalData.userCache;
        let initEmail = userCache["agentEmail"]? userCache["agentEmail"]: "";

        $SendMailPop.showSendMailPop({
            //初始邮箱
            initEmail: initEmail,

            onMailInput: (value)=> {
                email = value;
            },

            onConfirm: (e)=> {
                let errorMsg = RegexUtils.regexTypeWithMsg(email, constants.RegexTypes.LINKTELEMAIL);
                if (errorMsg) {
                    $Toast.showText(errorMsg);
                    return true;
                } else {
                    //保存代理商邮箱
                    saveUserCache({agentEmail: email});

                    this._requestSendBillEmail(email);
                    return false;
                }
            },

            onCancel: (e)=> {
            },
        });
    },

    /**
     * 加载更多事件
     */
    loadMore: function () {
        this._requestDetails();
    },

    /**
     * 查询明细数据
     * @private
     */
    _requestDetails() {
        let params = {
            date: profitDate,
            types: this.data.profitType.type,

            agentId: agentId,
            storeId: this.data.storeId,
            pageNo: pageNo,
            pageSize: pageSize
        };
        network_utils.buildRequest(constants.Interfaces.FUNC_SN_POS_DT, params, {
            doHttpSucess: (response) => {
                if (constants.RetCodes.SUCCESS === response.retCode) {
                    // let resultList = response["setAgentMerAccstatList"];
                    this._requestSuccess(response);
                    return true;
                }
                return false;
            }
        }).send();
    },

    /**
     * 请求成功
     * @param response
     * @private
     */
    _requestSuccess: function (response) {
        let subList = response["setAgentMerAccstatList"];
        let filterList = _getDetailList(subList);
        let hasMore = response["hasMore"]? response["hasMore"] === 1: false;
        // let hasMore = subList.length === parseInt(pageSize) * parseInt(pageNo);

        this.setData({
            // detailDataList: pageNo === "1" ? filterList : this.data.detailDataList.concat(filterList),
            detailDataList: filterList,
            hasMore: hasMore
        });

        if (!hasMore) {
            $Toast.showText("已经加载完所有数据");
        } else {
            //成功并且有更多的场合pageNo+1
            pageNo = (parseInt(pageNo) + 1).toString();
        }
    },

    _requestSendBillEmail(email) {
        let params = {
            email: email,
            date: profitDate,
            types: this.data.profitType.type,
            agentId: agentId,
            storeId: this.data.storeId
        };
        network_utils.buildRequest(constants.Interfaces.FUNC_PROFIT_SENDBILLEMAIL, params, {
            doHttpSucess: (response) => {
                if (constants.RetCodes.SUCCESS === response.retCode) {
                    $Toast.showText("分润明细邮件将在3-5分钟后送达，请注意查收");
                    // umfToast.showToast("分润明细邮件将在3-5分钟后送达，请注意查收");
                    return true;
                }
                return false;
            }
        }).send();
    }

});

function _getDetailList(resultList) {
    if (!resultList) {
        resultList = [];
    }

    let helpers = {
        convertList: (item)=> {
            let subArr1 = [],
                subArr2 = [];
            subArr1.push({title: "交易总额(元):", tag: "amount", value: item["amount"]? umfutils.fentoyuan(item["amount"]) : "0.0"});
            subArr1.push({title: "正向交易总额(元):", tag: "ptAmount", value: item["ptAmount"]? umfutils.fentoyuan(item["ptAmount"]) : "0.0" });
            subArr1.push({title: "反向交易总额(元):", tag: "ttamount", value: item["ttamount"]? umfutils.fentoyuan(item["ttamount"]) : "-0.0" });
            subArr1.push({title: "交易总笔数:", tag: "transnum", value: item["transnum"]? item["transnum"] : "0"});
            subArr1.push({title: "正向交易笔数:", tag: "ptnum", value: item["ptnum"]? item["ptnum"] : "0"});
            subArr1.push({title: "反向交易笔数:", tag: "ttnum", value: item["ttnum"]? item["ttnum"] : "0"});
            subArr1.push({title: "代理商编号:", tag: "agentId", value: item["agentId"]? item["agentId"] : ""});

            subArr2.push({title: "商户编号:", tag: "storeId", value: item["storeId"]});
            subArr2.push({title: "实际分润金额(元):", tag: "profitAmt", value: item["profitAmt"]? umfutils.fentoyuan(item["profitAmt"]) : "0.0"});
            subArr2.push({title: "全额分润金额(元):", tag: "fullProfitAmt", value: item["fullProfitAmt"]? umfutils.fentoyuan(item["fullProfitAmt"]) : "0.0"});
            subArr2.push({title: "非封顶分润金额(元):", tag: "uncapsAmt", value: item["uncapsAmt"]? umfutils.fentoyuan(item["uncapsAmt"]) : "0.0"});
            subArr2.push({title: "封顶未达分润金额(元):", tag: "capSunreacAmt", value: item["capSunreacAmt"]? umfutils.fentoyuan(item["capSunreacAmt"]) : "0.0"});
            subArr2.push({title: "封顶已达分润金额(元):", tag: "capSreachedAmt", value: item["capSreachedAmt"]? umfutils.fentoyuan(item["capSreachedAmt"]) : "0.0"});
            subArr2.push({title: "上级代理商:", tag: "upAgentId", value: item["upAgentId"]? item["upAgentId"]: ""});
            return {title: item["storeName"], subArr1: subArr1, subArr2: subArr2};
        }
    };

    let convertList = resultList.map(helpers.convertList);
    return convertList;
}